package thu.cs.segroup.parser;

import java.io.File;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;

import thu.cs.segroup.entity.TestCase;
import thu.cs.segroup.entity.TestCaseConfig;
import thu.cs.segroup.entity.TestSuite;
import thu.cs.segroup.util.Constants;

public class TestSuiteParser {

	public static TestSuite parseTestSuite(File test_suite_xml_file) {
		Document doc;
		SAXReader reader = new SAXReader();
		TestSuite suite = null;
		try {
			doc = reader.read(test_suite_xml_file);
			Element root = doc.getRootElement();
			Iterator iter = root.elementIterator();
			suite = new TestSuite();
			suite.setName(root.attributeValue(Constants.XML_TEST_SUITE_NAME));
			ArrayList<String> categories;
			while (iter.hasNext()) {
				Element element = (Element)iter.next();
				if (element.getName().equals(Constants.XML_TEST_SUITE_TESTCASE)) {
					categories = new ArrayList<String>();
					categories.add(element.getStringValue());
					suite.addStep(categories);
				} else if (element.getName().equals(Constants.XML_TEST_SUITE_CONCURRENCY)) {
					List<Element> con_cases = element.selectNodes(Constants.XML_TEST_SUITE_CONTESTCASE_PATH);
					categories = new ArrayList<String>();
					for (int i = 0; i < con_cases.size(); i++) {
						categories.add(con_cases.get(i).getStringValue());
					}
					suite.addStep(categories);
				} else if (element.getName().equals(Constants.XML_TEST_SUITE_CHANGE)) {
					suite.setChangtime(Long.parseLong(element.attributeValue(Constants.XML_TEST_SUITE_CHANGE_TIME)));
					suite.setNext(element.attributeValue(Constants.XML_TEST_SUITE_CHANGE_NEXT));
				}
			}
		} catch (DocumentException e) {
			e.printStackTrace();
			return null;
		}
		return suite;
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Document doc;
		SAXReader reader = new SAXReader();
		try {
			doc = reader.read(new File("data/suite/1.xml"));
			Element root = doc.getRootElement();
			Iterator iter = root.elementIterator();
			TestSuite suite = new TestSuite();
			suite.setName(root.attributeValue(Constants.XML_TEST_SUITE_NAME));
			ArrayList<String> categories;
			while (iter.hasNext()) {
				Element element = (Element)iter.next();
				if (element.getName().equals(Constants.XML_TEST_SUITE_TESTCASE)) {
					categories = new ArrayList<String>();
					categories.add(element.getStringValue());
					suite.addStep(categories);
				} else if (element.getName().equals(Constants.XML_TEST_SUITE_CONCURRENCY)) {
					List<Element> con_cases = element.selectNodes(Constants.XML_TEST_SUITE_CONTESTCASE_PATH);
					categories = new ArrayList<String>();
					for (int i = 0; i < con_cases.size(); i++) {
						categories.add(con_cases.get(i).getStringValue());
					}
					suite.addStep(categories);
				} else if (element.getName().equals(Constants.XML_TEST_SUITE_CHANGE)) {
					suite.setChangtime(Long.parseLong(element.attributeValue(Constants.XML_TEST_SUITE_CHANGE_TIME)));
					suite.setNext(element.attributeValue(Constants.XML_TEST_SUITE_CHANGE_NEXT));
				}
			}
			System.out.println("OVER!");
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}
